<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>AI配置管理</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Microsoft YaHei', sans-serif;
            background: #f5f5f5;
            color: #333;
            min-height: 100vh;
        }

        /* 主内容区 */
        .main-content {
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
            overflow-y: auto;
        }

        .header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 20px;
            padding-bottom: 10px;
            border-bottom: 1px solid #ddd;
            position: relative;
        }

        .page-title-container {
            display: flex;
            align-items: center;
        }

        .back-btn {
            display: flex;
            align-items: center;
            justify-content: center;
            background: #3498db;
            color: white;
            width: 36px;
            height: 36px;
            border-radius: 50%;
            margin-right: 15px;
            cursor: pointer;
            transition: all 0.3s;
            text-decoration: none;
            font-size: 18px;
            box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
        }

        .back-btn:hover {
            background: #2980b9;
            transform: translateX(-3px);
        }

        .page-title {
            font-size: 24px;
            font-weight: bold;
        }

        .user-info {
            display: flex;
            align-items: center;
        }

        .user-avatar {
            width: 40px;
            height: 40px;
            border-radius: 50%;
            margin-right: 10px;
            background: #3498db;
            color: white;
            display: flex;
            align-items: center;
            justify-content: center;
            font-weight: bold;
        }

        .user-name {
            font-weight: bold;
        }

        /* 标签页 */
        .tabs {
            display: flex;
            border-bottom: 1px solid #ddd;
            margin-bottom: 20px;
        }

        .tab {
            padding: 12px 20px;
            cursor: pointer;
            transition: all 0.3s;
            font-size: 16px;
            font-weight: bold;
            border-bottom: 3px solid transparent;
        }

        .tab.active {
            color: #3498db;
            border-bottom-color: #3498db;
        }

        .tab:hover {
            background: #f0f0f0;
        }

        /* 内容区 */
        .tab-content {
            display: none;
        }

        .tab-content.active {
            display: block;
        }

        /* 案件类型配置 */
        .config-panel {
            background: white;
            border-radius: 5px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
            padding: 20px;
            margin-bottom: 20px;
        }

        .panel-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 20px;
        }

        .panel-title {
            font-size: 18px;
            font-weight: bold;
        }

        .add-btn {
            background: #3498db;
            color: white;
            border: none;
            padding: 8px 15px;
            border-radius: 4px;
            cursor: pointer;
            transition: all 0.3s;
            display: flex;
            align-items: center;
        }

        .add-btn:hover {
            background: #2980b9;
        }

        .add-btn-icon {
            margin-right: 5px;
            font-size: 16px;
        }

        /* 表格样式 */
        .config-table {
            width: 100%;
            border-collapse: collapse;
        }

        .config-table th,
        .config-table td {
            padding: 12px 15px;
            text-align: left;
            border-bottom: 1px solid #ddd;
        }

        .config-table th {
            font-weight: bold;
            background: #f8f9fa;
        }

        .config-table tr:hover {
            background: #f5f5f5;
        }

        /* 开关按钮 */
        .switch {
            position: relative;
            display: inline-block;
            width: 50px;
            height: 24px;
        }

        .switch input {
            opacity: 0;
            width: 0;
            height: 0;
        }

        .slider {
            position: absolute;
            cursor: pointer;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background-color: #ccc;
            transition: .4s;
            border-radius: 24px;
        }

        .slider:before {
            position: absolute;
            content: "";
            height: 16px;
            width: 16px;
            left: 4px;
            bottom: 4px;
            background-color: white;
            transition: .4s;
            border-radius: 50%;
        }

        input:checked + .slider {
            background-color: #2ecc71;
        }

        input:focus + .slider {
            box-shadow: 0 0 1px #2ecc71;
        }

        input:checked + .slider:before {
            transform: translateX(26px);
        }

        /* 操作按钮 */
        .action-btn {
            padding: 6px 12px;
            margin-right: 5px;
            border-radius: 4px;
            font-size: 14px;
            cursor: pointer;
            transition: all 0.3s;
            border: none;
        }

        .edit-btn {
            background: #3498db;
            color: white;
        }

        .edit-btn:hover {
            background: #2980b9;
        }

        .delete-btn {
            background: #e74c3c;
            color: white;
        }

        .delete-btn:hover {
            background: #c0392b;
        }

        /* 弹窗样式 */
        .modal {
            display: none;
            position: fixed;
            z-index: 1000;
            left: 0;
            top: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, 0.5);
        }

        .modal-content {
            background-color: white;
            margin: 10% auto;
            padding: 20px;
            border-radius: 5px;
            width: 500px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);
            position: relative;
        }

        .close-btn {
            position: absolute;
            right: 20px;
            top: 15px;
            font-size: 24px;
            cursor: pointer;
            color: #aaa;
        }

        .close-btn:hover {
            color: #333;
        }

        .modal-title {
            font-size: 20px;
            font-weight: bold;
            margin-bottom: 20px;
            padding-bottom: 10px;
            border-bottom: 1px solid #ddd;
        }

        .form-group {
            margin-bottom: 15px;
        }

        .form-label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }

        .form-input {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 14px;
        }

        .form-input:focus {
            border-color: #3498db;
            outline: none;
            box-shadow: 0 0 5px rgba(52, 152, 219, 0.5);
        }

        .form-actions {
            display: flex;
            justify-content: flex-end;
            margin-top: 20px;
        }

        .form-btn {
            padding: 8px 15px;
            border-radius: 4px;
            font-size: 14px;
            cursor: pointer;
            transition: all 0.3s;
            border: none;
            margin-left: 10px;
        }

        .cancel-btn {
            background: #95a5a6;
            color: white;
        }

        .cancel-btn:hover {
            background: #7f8c8d;
        }

        .save-btn {
            background: #2ecc71;
            color: white;
        }

        .save-btn:hover {
            background: #27ae60;
        }

        /* 空状态 */
        .empty-state {
            text-align: center;
            padding: 40px 0;
            color: #7f8c8d;
        }

        .empty-icon {
            font-size: 48px;
            margin-bottom: 15px;
        }

        .empty-text {
            font-size: 16px;
            margin-bottom: 20px;
        }
    </style>
</head>
<body>
    <!-- 主内容区 -->
    <div class="main-content">
        <div class="header">
            <div class="page-title-container">
                <a href="AI文书首页.html" class="back-btn">←</a>
                <div class="page-title">AI配置管理</div>
            </div>
            <div class="user-info">
                <div class="user-avatar">A</div>
                <div class="user-name">管理员</div>
            </div>
        </div>

        <!-- 标签页 -->
        <div class="tabs">
            <div class="tab active" data-tab="case-type">案件类型配置</div>
            <div class="tab" data-tab="appraisal-items">鉴定事项</div>
            <div class="tab" data-tab="appraisal-standards">鉴定标准</div>
            <div class="tab" data-tab="appraisal-tools">鉴定工具</div>
            <div class="tab" data-tab="other-config">其他配置</div>
        </div>

        <!-- 标签内容 -->
        <div class="tab-content active" id="case-type-content">
            <div class="config-panel">
                <div class="panel-header">
                    <div class="panel-title">案件类型列表</div>
                </div>
                
                <table class="config-table" id="case-type-table">
                    <thead>
                        <tr>
                            <th style="width: 60px;">序号</th>
                            <th>案件类型名称</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td>1</td>
                            <td>法医临床鉴定</td>
                        </tr>
                        <tr>
                            <td>2</td>
                            <td>法医病理鉴定</td>
                        </tr>
                        <tr>
                            <td>3</td>
                            <td>医疗损害鉴定</td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>

        <!-- 鉴定事项标签内容 -->
        <div class="tab-content" id="appraisal-items-content">
            <div class="config-panel">
                <div class="panel-header">
                    <div class="panel-title">鉴定事项列表</div>
                    <button class="add-btn" id="add-appraisal-item">
                        <span class="add-btn-icon">+</span>添加鉴定事项
                    </button>
                </div>
                
                <div class="form-group" style="margin-bottom: 20px;">
                    <label class="form-label" for="case-type-filter">选择案件类型</label>
                    <select class="form-input" id="case-type-filter">
                        <option value="">全部案件类型</option>
                        <option value="法医临床鉴定">法医临床鉴定</option>
                        <option value="法医病理鉴定">法医病理鉴定</option>
                        <option value="医疗损害鉴定">医疗损害鉴定</option>
                    </select>
                </div>
                
                <table class="config-table" id="appraisal-items-table">
                    <thead>
                        <tr>
                            <th style="width: 60px;">序号</th>
                            <th style="width: 150px;">案件类型</th>
                            <th>鉴定事项名称</th>
                            <th style="width: 100px;">状态</th>
                            <th style="width: 150px;">操作</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td>1</td>
                            <td>法医临床鉴定</td>
                            <td>伤残等级评定</td>
                            <td>
                                <label class="switch">
                                    <input type="checkbox" checked>
                                    <span class="slider"></span>
                                </label>
                            </td>
                            <td>
                                <button class="action-btn edit-btn">编辑</button>
                                <button class="action-btn delete-btn">删除</button>
                            </td>
                        </tr>
                        <tr>
                            <td>2</td>
                            <td>法医病理鉴定</td>
                            <td>死亡原因分析</td>
                            <td>
                                <label class="switch">
                                    <input type="checkbox" checked>
                                    <span class="slider"></span>
                                </label>
                            </td>
                            <td>
                                <button class="action-btn edit-btn">编辑</button>
                                <button class="action-btn delete-btn">删除</button>
                            </td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>

        <!-- 鉴定标准标签内容 -->
        <div class="tab-content" id="appraisal-standards-content">
            <div class="config-panel">
                <div class="panel-header">
                    <div class="panel-title">鉴定标准列表</div>
                    <button class="add-btn" id="add-appraisal-standard">
                        <span class="add-btn-icon">+</span>添加鉴定标准
                    </button>
                </div>
                <table class="config-table" id="appraisal-standards-table">
                    <thead>
                        <tr>
                            <th style="width: 60px;">序号</th>
                            <th>鉴定标准名称</th>
                            <th style="width: 150px;">关联案件类型</th>
                            <th style="width: 100px;">状态</th>
                            <th style="width: 150px;">操作</th>
                        </tr>
                    </thead>
                    <tbody>
                        <!-- 动态添加鉴定标准 -->
                        <tr>
                            <td>1</td>
                            <td>标准示例1</td>
                            <td>法医临床鉴定</td>
                            <td>
                                <label class="switch">
                                    <input type="checkbox" checked>
                                    <span class="slider"></span>
                                </label>
                            </td>
                            <td>
                                <button class="action-btn edit-btn">编辑</button>
                                <button class="action-btn delete-btn">删除</button>
                            </td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>

        <!-- 鉴定工具标签内容 (existing, ensure headers are correct) -->
        <div class="tab-content" id="appraisal-tools-content">
            <div class="config-panel">
                <div class="panel-header">
                    <div class="panel-title">鉴定工具列表</div>
                    <button class="add-btn" id="add-appraisal-tool">
                        <span class="add-btn-icon">+</span>添加鉴定工具
                    </button>
                </div>
                <table class="config-table" id="appraisal-tools-table">
                    <thead>
                        <tr>
                            <th style="width: 60px;">序号</th>
                            <th>鉴定工具名称</th>
                            <th style="width: 150px;">关联案件类型</th>
                            <th style="width: 100px;">状态</th>
                            <th style="width: 150px;">操作</th>
                        </tr>
                    </thead>
                    <tbody>
                        <!-- 动态添加鉴定工具 -->
                         <tr>
                            <td>1</td>
                            <td>工具示例1</td>
                            <td>法医病理鉴定</td>
                            <td>
                                <label class="switch">
                                    <input type="checkbox" checked>
                                    <span class="slider"></span>
                                </label>
                            </td>
                            <td>
                                <button class="action-btn edit-btn">编辑</button>
                                <button class="action-btn delete-btn">删除</button>
                            </td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>

        <div class="tab-content" id="other-config-content">
            <!-- Content for other-config tab can be added here -->
        </div>
    </div>

    <!-- 添加/编辑案件类型弹窗 -->
    <div class="modal" id="case-type-modal">
        <div class="modal-content">
            <span class="close-btn">&times;</span>
            <h3 class="modal-title" id="modal-title">添加案件类型</h3>
            <form id="case-type-form">
                <input type="hidden" id="edit-id" value="">
                <div class="form-group">
                    <label class="form-label" for="case-type-name">案件类型名称</label>
                    <input type="text" class="form-input" id="case-type-name" placeholder="请输入案件类型名称" required>
                </div>
                <div class="form-group">
                    <label class="form-label">状态</label>
                    <label class="switch" style="margin-top: 5px;">
                        <input type="checkbox" id="case-type-status" checked>
                        <span class="slider"></span>
                    </label>
                    <span style="margin-left: 10px; vertical-align: middle;" id="status-text">启用</span>
                </div>
                <!-- Removed related-tools form-group from here -->
                <div class="form-actions">
                    <button type="button" class="form-btn cancel-btn" id="cancel-btn">取消</button>
                    <button type="submit" class="form-btn save-btn">保存</button>
                </div>
            </form>
        </div>
    </div>

    <!-- 添加/编辑鉴定标准弹窗 -->
    <div class="modal" id="appraisal-standard-modal">
        <div class="modal-content">
            <span class="close-btn">&times;</span>
            <h3 class="modal-title">添加鉴定标准</h3>
            <form id="appraisal-standard-form">
                <input type="hidden" id="edit-standard-id" value="">
                <div class="form-group">
                    <label class="form-label" for="standard-name">鉴定标准名称</label>
                    <input type="text" class="form-input" id="standard-name" placeholder="请输入鉴定标准名称" required>
                </div>
                <div class="form-group">
                    <label class="form-label" for="standard-associated-case-type">关联案件类型</label>
                    <select class="form-input" id="standard-associated-case-type" required>
                        <option value="">请选择案件类型</option>
                        <option value="法医临床鉴定">法医临床鉴定</option>
                        <option value="法医病理鉴定">法医病理鉴定</option>
                        <option value="医疗损害鉴定">医疗损害鉴定</option>
                        <!-- 更多案件类型可以动态加载 -->
                    </select>
                </div>
                <div class="form-group">
                    <label class="form-label">状态</label>
                    <label class="switch" style="margin-top: 5px;">
                        <input type="checkbox" id="standard-status" checked>
                        <span class="slider"></span>
                    </label>
                    <span style="margin-left: 10px; vertical-align: middle;" class="status-text-dynamic">启用</span>
                </div>
                <div class="form-actions">
                    <button type="button" class="form-btn cancel-btn">取消</button>
                    <button type="submit" class="form-btn save-btn">保存</button>
                </div>
            </form>
        </div>
    </div>

    <!-- 添加/编辑鉴定工具弹窗 -->
    <div class="modal" id="appraisal-tool-modal">
        <div class="modal-content">
            <span class="close-btn">&times;</span>
            <h3 class="modal-title">添加鉴定工具</h3>
            <form id="appraisal-tool-form">
                <input type="hidden" id="edit-tool-id" value="">
                <div class="form-group">
                    <label class="form-label" for="tool-name">鉴定工具名称</label>
                    <input type="text" class="form-input" id="tool-name" placeholder="请输入鉴定工具名称" required>
                </div>
                <div class="form-group">
                    <label class="form-label" for="tool-associated-case-type">关联案件类型</label>
                    <select class="form-input" id="tool-associated-case-type" required>
                        <option value="">请选择案件类型</option>
                        <option value="法医临床鉴定">法医临床鉴定</option>
                        <option value="法医病理鉴定">法医病理鉴定</option>
                        <option value="医疗损害鉴定">医疗损害鉴定</option>
                        <!-- 更多案件类型可以动态加载 -->
                    </select>
                </div>
                <div class="form-group">
                    <label class="form-label">状态</label>
                    <label class="switch" style="margin-top: 5px;">
                        <input type="checkbox" id="tool-status" checked>
                        <span class="slider"></span>
                    </label>
                    <span style="margin-left: 10px; vertical-align: middle;" class="status-text-dynamic">启用</span>
                </div>
                <div class="form-actions">
                    <button type="button" class="form-btn cancel-btn">取消</button>
                    <button type="submit" class="form-btn save-btn">保存</button>
                </div>
            </form>
        </div>
    </div>

    <script>
        // 标签页切换
        const tabs = document.querySelectorAll('.tab');
        tabs.forEach(tab => {
            tab.addEventListener('click', function() {
                // 移除所有标签的激活状态
                tabs.forEach(t => t.classList.remove('active'));
                // 激活当前标签
                this.classList.add('active');
                
                // 隐藏所有内容
                document.querySelectorAll('.tab-content').forEach(content => {
                    content.classList.remove('active');
                });
                
                // 显示对应内容
                const tabId = this.getAttribute('data-tab');
                document.getElementById(tabId + '-content').classList.add('active');
            });
        });

        // 获取弹窗元素
        const modal = document.getElementById('case-type-modal');
        const closeBtn = document.querySelector('.close-btn');
        const cancelBtn = document.getElementById('cancel-btn');
        const form = document.getElementById('case-type-form');
        const caseTypeNameInput = document.getElementById('case-type-name');
        const caseTypeStatusInput = document.getElementById('case-type-status');
        const statusText = document.getElementById('status-text');
        const modalTitle = document.getElementById('modal-title');
        const editIdInput = document.getElementById('edit-id');

        // 新增：获取鉴定标准相关元素
        const appraisalStandardModal = document.getElementById('appraisal-standard-modal');
        const addAppraisalStandardBtn = document.getElementById('add-appraisal-standard');
        const appraisalStandardForm = document.getElementById('appraisal-standard-form');
        const appraisalStandardsTable = document.getElementById('appraisal-standards-table').getElementsByTagName('tbody')[0];
        const standardNameInput = document.getElementById('standard-name');
        const standardAssociatedCaseTypeSelect = document.getElementById('standard-associated-case-type');
        const standardStatusInput = document.getElementById('standard-status');
        const editStandardIdInput = document.getElementById('edit-standard-id');

        // 新增：获取鉴定工具相关元素
        const appraisalToolModal = document.getElementById('appraisal-tool-modal');
        const addAppraisalToolBtn = document.getElementById('add-appraisal-tool');
        const appraisalToolForm = document.getElementById('appraisal-tool-form');
        const appraisalToolsTable = document.getElementById('appraisal-tools-table').getElementsByTagName('tbody')[0];
        const toolNameInput = document.getElementById('tool-name');
        const toolAssociatedCaseTypeSelect = document.getElementById('tool-associated-case-type');
        const toolStatusInput = document.getElementById('tool-status');
        const editToolIdInput = document.getElementById('edit-tool-id');


        // Helper function to handle status text for dynamic modals
        function updateStatusText(checkboxElement, textElement) {
            textElement.textContent = checkboxElement.checked ? '启用' : '禁用';
        }
        
        document.querySelectorAll('.status-text-dynamic').forEach(el => {
            const checkbox = el.previousElementSibling.querySelector('input[type="checkbox"]');
            if (checkbox) {
                updateStatusText(checkbox, el); // Initial update
                checkbox.addEventListener('change', function() {
                    updateStatusText(this, el);
                });
            }
        });


        // --- 鉴定标准逻辑 ---
        addAppraisalStandardBtn.addEventListener('click', function() {
            appraisalStandardModal.querySelector('.modal-title').textContent = '添加鉴定标准';
            appraisalStandardForm.reset(); // Resets form fields
            standardStatusInput.checked = true; // Default to checked
            updateStatusText(standardStatusInput, appraisalStandardModal.querySelector('.status-text-dynamic'));
            editStandardIdInput.value = '';
            appraisalStandardModal.style.display = 'block';
        });

        appraisalStandardForm.addEventListener('submit', function(e) {
            e.preventDefault();
            const standardName = standardNameInput.value.trim();
            const associatedCaseType = standardAssociatedCaseTypeSelect.value;
            const isEnabled = standardStatusInput.checked;
            const editId = editStandardIdInput.value;

            if (!standardName || !associatedCaseType) {
                alert('请输入鉴定标准名称并选择关联案件类型');
                return;
            }

            if (editId) { // 编辑逻辑 (简化版，实际应用中需要通过ID找到对应行)
                const row = Array.from(appraisalStandardsTable.rows).find(r => r.dataset.id === editId); // Assume row has data-id
                if (row) {
                    row.cells[1].textContent = standardName;
                    row.cells[2].textContent = associatedCaseType;
                    row.cells[3].querySelector('input').checked = isEnabled;
                }
            } else { // 添加逻辑
                const rowCount = appraisalStandardsTable.rows.length;
                const newRow = appraisalStandardsTable.insertRow();
                newRow.dataset.id = `std-${Date.now()}`; // Example ID
                newRow.innerHTML = `
                    <td>${rowCount + 1}</td>
                    <td>${standardName}</td>
                    <td>${associatedCaseType}</td>
                    <td>
                        <label class="switch">
                            <input type="checkbox" ${isEnabled ? 'checked' : ''}>
                            <span class="slider"></span>
                        </label>
                    </td>
                    <td>
                        <button class="action-btn edit-btn">编辑</button>
                        <button class="action-btn delete-btn">删除</button>
                    </td>
                `;
                addEditDeleteListenersToRow(newRow, appraisalStandardModal, appraisalStandardForm, {
                    nameInput: standardNameInput,
                    caseTypeSelect: standardAssociatedCaseTypeSelect,
                    statusInput: standardStatusInput,
                    idInput: editStandardIdInput,
                    modalTitleText: '编辑鉴定标准'
                });
            }
            appraisalStandardModal.style.display = 'none';
        });

        // --- 鉴定工具逻辑 ---
        addAppraisalToolBtn.addEventListener('click', function() {
            appraisalToolModal.querySelector('.modal-title').textContent = '添加鉴定工具';
            appraisalToolForm.reset();
            toolStatusInput.checked = true;
            updateStatusText(toolStatusInput, appraisalToolModal.querySelector('.status-text-dynamic'));
            editToolIdInput.value = '';
            appraisalToolModal.style.display = 'block';
        });

        appraisalToolForm.addEventListener('submit', function(e) {
            e.preventDefault();
            const toolName = toolNameInput.value.trim();
            const associatedCaseType = toolAssociatedCaseTypeSelect.value;
            const isEnabled = toolStatusInput.checked;
            const editId = editToolIdInput.value;

            if (!toolName || !associatedCaseType) {
                alert('请输入鉴定工具名称并选择关联案件类型');
                return;
            }
            
            if (editId) { // 编辑逻辑
                const row = Array.from(appraisalToolsTable.rows).find(r => r.dataset.id === editId);
                 if (row) {
                    row.cells[1].textContent = toolName;
                    row.cells[2].textContent = associatedCaseType;
                    row.cells[3].querySelector('input').checked = isEnabled;
                }
            } else { // 添加逻辑
                const rowCount = appraisalToolsTable.rows.length;
                const newRow = appraisalToolsTable.insertRow();
                newRow.dataset.id = `tool-${Date.now()}`; // Example ID
                newRow.innerHTML = `
                    <td>${rowCount + 1}</td>
                    <td>${toolName}</td>
                    <td>${associatedCaseType}</td>
                    <td>
                        <label class="switch">
                            <input type="checkbox" ${isEnabled ? 'checked' : ''}>
                            <span class="slider"></span>
                        </label>
                    </td>
                    <td>
                        <button class="action-btn edit-btn">编辑</button>
                        <button class="action-btn delete-btn">删除</button>
                    </td>
                `;
                 addEditDeleteListenersToRow(newRow, appraisalToolModal, appraisalToolForm, {
                    nameInput: toolNameInput,
                    caseTypeSelect: toolAssociatedCaseTypeSelect,
                    statusInput: toolStatusInput,
                    idInput: editToolIdInput,
                    modalTitleText: '编辑鉴定工具'
                });
            }
            appraisalToolModal.style.display = 'none';
        });

        // 通用关闭弹窗逻辑 (确保所有弹窗的关闭按钮和取消按钮都有这个类或相似处理)
        document.querySelectorAll('.modal .close-btn, .modal .cancel-btn').forEach(btn => {
            btn.addEventListener('click', function() {
                this.closest('.modal').style.display = 'none';
            });
        });
        
        // 通用点击弹窗外部关闭
        window.addEventListener('click', function(event) {
            if (event.target.classList.contains('modal')) {
                event.target.style.display = 'none';
            }
        });

        // 通用函数为新行添加编辑和删除事件监听器
        function addEditDeleteListenersToRow(row, modalElement, formElement, config) {
            const editBtn = row.querySelector('.edit-btn');
            const deleteBtn = row.querySelector('.delete-btn');

            editBtn.addEventListener('click', function() {
                modalElement.querySelector('.modal-title').textContent = config.modalTitleText;
                config.idInput.value = row.dataset.id; // Store ID for editing
                config.nameInput.value = row.cells[1].textContent;
                config.caseTypeSelect.value = row.cells[2].textContent;
                config.statusInput.checked = row.cells[3].querySelector('input').checked;
                updateStatusText(config.statusInput, modalElement.querySelector('.status-text-dynamic'));
                modalElement.style.display = 'block';
            });

            deleteBtn.addEventListener('click', function() {
                if (confirm('确定要删除此项吗？')) {
                    row.remove();
                    // Re-number rows if necessary (optional)
                }
            });
        }
        
        // 为已存在的行添加事件监听器 (示例，实际应在页面加载时或动态添加行时调用)
        document.querySelectorAll('#appraisal-standards-table tbody tr').forEach(row => {
             if (row.cells.length > 1) { // Ensure it's not an empty placeholder row
                addEditDeleteListenersToRow(row, appraisalStandardModal, appraisalStandardForm, {
                    nameInput: standardNameInput,
                    caseTypeSelect: standardAssociatedCaseTypeSelect,
                    statusInput: standardStatusInput,
                    idInput: editStandardIdInput,
                    modalTitleText: '编辑鉴定标准'
                });
            }
        });
        document.querySelectorAll('#appraisal-tools-table tbody tr').forEach(row => {
            if (row.cells.length > 1) {
                addEditDeleteListenersToRow(row, appraisalToolModal, appraisalToolForm, {
                    nameInput: toolNameInput,
                    caseTypeSelect: toolAssociatedCaseTypeSelect,
                    statusInput: toolStatusInput,
                    idInput: editToolIdInput,
                    modalTitleText: '编辑鉴定工具'
                });
            }
        });
    </script>

    <script>
        // 简要案情相关
        const briefModal = document.getElementById('case-brief-modal');
        const briefCloseBtn = document.getElementById('brief-close-btn');
        const briefCancelBtn = document.getElementById('brief-cancel-btn');
        const addBriefBtn = document.getElementById('add-case-brief');
        const briefForm = document.getElementById('case-brief-form');
        const briefCaseTypeSelect = document.getElementById('brief-case-type');
        const briefDescriptionInput = document.getElementById('brief-description');
        const briefStatusInput = document.getElementById('brief-status');
        const briefStatusText = document.getElementById('brief-status-text');
        const briefModalTitle = document.getElementById('brief-modal-title');
        const briefEditIdInput = document.getElementById('brief-edit-id');
        const briefTable = document.getElementById('case-brief-table').getElementsByTagName('tbody')[0];
        const briefCaseTypeFilter = document.getElementById('brief-case-type-filter');

        // 打开添加简要案情弹窗
        addBriefBtn.addEventListener('click', function() {
            briefModalTitle.textContent = '添加简要案情';
            briefCaseTypeSelect.value = '';
            briefDescriptionInput.value = '';
            briefStatusInput.checked = true;
            briefStatusText.textContent = '启用';
            briefEditIdInput.value = '';
            briefModal.style.display = 'block';
        });

        // 关闭简要案情弹窗
        briefCloseBtn.addEventListener('click', function() {
            briefModal.style.display = 'none';
        });

        briefCancelBtn.addEventListener('click', function() {
            briefModal.style.display = 'none';
        });

        // 点击弹窗外部关闭弹窗
        window.addEventListener('click', function(event) {
            if (event.target == briefModal) {
                briefModal.style.display = 'none';
            }
        });

        // 状态切换文字显示
        briefStatusInput.addEventListener('change', function() {
            briefStatusText.textContent = this.checked ? '启用' : '禁用';
        });

        // 简要案情表单提交处理
        briefForm.addEventListener('submit', function(e) {
            e.preventDefault();
            
            const caseType = briefCaseTypeSelect.value.trim();
            const description = briefDescriptionInput.value.trim();
            const isEnabled = briefStatusInput.checked;
            const editId = briefEditIdInput.value;
            
            if (!caseType) {
                alert('请选择案件类型');
                return;
            }
            
            if (!description) {
                alert('请输入简要案情描述');
                return;
            }
            
            if (editId) {
                // 编辑现有简要案情
                const row = briefTable.rows[parseInt(editId) - 1];
                row.cells[1].textContent = caseType;
                row.cells[2].textContent = description;
                row.cells[3].querySelector('input').checked = isEnabled;
            } else {
                // 添加新简要案情
                const newRow = briefTable.insertRow();
                
                const cellIndex = newRow.insertCell(0);
                const cellType = newRow.insertCell(1);
                const cellDesc = newRow.insertCell(2);
                const cellStatus = newRow.insertCell(3);
                const cellAction = newRow.insertCell(4);
                
                cellIndex.textContent = briefTable.rows.length;
                cellType.textContent = caseType;
                cellDesc.textContent = description;
                
                cellStatus.innerHTML = `
                    <label class="switch">
                        <input type="checkbox" ${isEnabled ? 'checked' : ''}>
                        <span class="slider"></span>
                    </label>
                `;
                
                cellAction.innerHTML = `
                    <button class="action-btn edit-btn">编辑</button>
                    <button class="action-btn delete-btn">删除</button>
                `;
                
                // 绑定新添加的按钮事件
                const editBtn = cellAction.querySelector('.edit-btn');
                const deleteBtn = cellAction.querySelector('.delete-btn');
                
                editBtn.addEventListener('click', function() {
                    const row = this.closest('tr');
                    const rowIndex = row.rowIndex;
                    const caseType = row.cells[1].textContent;
                    const description = row.cells[2].textContent;
                    const isEnabled = row.cells[3].querySelector('input').checked;
                    
                    briefModalTitle.textContent = '编辑简要案情';
                    briefCaseTypeSelect.value = caseType;
                    briefDescriptionInput.value = description;
                    briefStatusInput.checked = isEnabled;
                    briefStatusText.textContent = isEnabled ? '启用' : '禁用';
                    briefEditIdInput.value = rowIndex;
                    
                    briefModal.style.display = 'block';
                });
                
                deleteBtn.addEventListener('click', function() {
                    if (confirm('确定要删除该简要案情吗？')) {
                        const row = this.closest('tr');
                        row.remove();
                        
                        // 更新序号
                        for (let i = 0; i < briefTable.rows.length; i++) {
                            briefTable.rows[i].cells[0].textContent = i + 1;
                        }
                    }
                });
            }
            
            // 关闭弹窗
            briefModal.style.display = 'none';
        });

        // 绑定已有的简要案情编辑和删除按钮
        document.querySelectorAll('#case-brief-table .edit-btn').forEach(btn => {
            btn.addEventListener('click', function() {
                const row = this.closest('tr');
                const rowIndex = row.rowIndex;
                const caseType = row.cells[1].textContent;
                const description = row.cells[2].textContent;
                const isEnabled = row.cells[3].querySelector('input').checked;
                
                briefModalTitle.textContent = '编辑简要案情';
                briefCaseTypeSelect.value = caseType;
                briefDescriptionInput.value = description;
                briefStatusInput.checked = isEnabled;
                briefStatusText.textContent = isEnabled ? '启用' : '禁用';
                briefEditIdInput.value = rowIndex;
                
                briefModal.style.display = 'block';
            });
        });

        document.querySelectorAll('#case-brief-table .delete-btn').forEach(btn => {
            btn.addEventListener('click', function() {
                if (confirm('确定要删除该简要案情吗？')) {
                    const row = this.closest('tr');
                    row.remove();
                    
                    // 更新序号
                    for (let i = 0; i < briefTable.rows.length; i++) {
                        briefTable.rows[i].cells[0].textContent = i + 1;
                    }
                }
            });
        });

        // 案件类型筛选
        briefCaseTypeFilter.addEventListener('change', function() {
            const selectedType = this.value;
            
            document.querySelectorAll('#case-brief-table tbody tr').forEach(row => {
                const rowType = row.cells[1].textContent;
                
                if (!selectedType || rowType === selectedType) {
                    row.style.display = '';
                } else {
                    row.style.display = 'none';
                }
            });
        });
    </script>
</body>
</html>